package 数组;

/**
 * @author TTDB
 * @create 2025/9/14
 */
public class productExceptSelf {
    public int[] productExceptSelf(int[] nums) {
        //从前往后乘，从后往前乘，然后相乘
        int[] res = new int[nums.length];
        int n = nums.length;
        int[] lx = new int[n];
        lx[0] = 1;
        for (int i = 1; i < n; i++) {
            lx[i] = lx[i - 1] * nums[i - 1];
        }
        int[] rx = new int[n];
        rx[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            rx[i] = rx[i + 1] * nums[i + 1];
        }
        for (int i = 0; i < n; i++) {
            res[i] = lx[i] * rx[i];
        }
        return res;
    }
}
